﻿//  --------------------------------
//  Copyright (c) AW Systems, Inc. All rights reserved.
//  This source code is made available under the terms of the Microsoft Public License (Ms-PL)
//  http://www.codeplex.com/SLAM/license
//  ---------------------------------

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Collections;

namespace AWS.SLAM.Hierarchy
{
    public partial class HierarchyPositionFieldEditor : UserControl, IFieldEditor
    {
        private string itemDisplayName;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        #region IFieldEditor Members

        public bool DisplayAsNewSection
        {
            get { return true; }
        }

        public void InitializeWithField(SPField field)
        {
            Dictionary<string, string> fieldNames = new Dictionary<string, string>();

            SPList parentList = null;

            if (Request.QueryString["List"] != null)
            {
                SPWeb currentWeb = SPControl.GetContextWeb(Context);
                parentList = currentWeb.Lists[new Guid(Request.QueryString["List"])];
            }
            else
            {
                parentList = field.ParentList;
            }
            if (parentList != null)
            {
                foreach (SPField listField in parentList.Fields)
                {
                    if (!listField.Hidden && listField.Reorderable)
                    {
                        fieldNames.Add(listField.Title, listField.InternalName);
                    }
                }
            }
            else
            {
                fieldNames.Add("Title", "Title");
            }

            foreach(KeyValuePair<string, string> fieldName in fieldNames)
            {
                ddlItemDisplayNameField.Items.Add(new ListItem(fieldName.Key, fieldName.Value));
            }

            if (!Page.IsPostBack)
            {
                HierarchyPositionField hierarchyField = field as HierarchyPositionField;
                if (hierarchyField != null)
                {
                    itemDisplayName = hierarchyField.ItemDisplayName;
                }

                ListItem selectedItem = ddlItemDisplayNameField.Items.FindByValue(itemDisplayName);
                if (selectedItem == null)
                {   
                    selectedItem = ddlItemDisplayNameField.Items.FindByValue("Title");
                }

                if (selectedItem != null)
                {
                    selectedItem.Selected = true;
                }
            }
        }

        public void OnSaveChange(SPField field, bool isNewField)
        {
            itemDisplayName = ddlItemDisplayNameField.SelectedValue;

            HierarchyPositionField hierarchyField = field as HierarchyPositionField;

            if (isNewField)
            {
                hierarchyField.UpdateItemDisplayNameProperty(itemDisplayName);
            }
            else
            {
                hierarchyField.ItemDisplayName = itemDisplayName;
            }
        }

        #endregion
    }
}